package com.baizhi.controller;

import com.baizhi.entity.Emp;
import com.baizhi.service.EmpServiceImpl;
import com.baizhi.utils.ALiYunOSSUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
public class Empcontroller {
    @Autowired
    private EmpServiceImpl empService;
    @ResponseBody
    @GetMapping("/emp/findAll")
    public List<Emp> findAll(){
        List<Emp> emps = empService.queryAll();
       return emps;
    }
    @ResponseBody
    @GetMapping("/emp/delete")
    public  HashMap<String, Object>  delete(String id){
        HashMap<String, Object> map = new HashMap<>();
        try {
             empService.delete(id);
             map.put("msg","删除员工信息成功");
             map.put("state",true);
             return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg","删除员工信息失败");
            map.put("state",false);
            throw new RuntimeException();
        }
    }
    @ResponseBody
    @GetMapping("/emp/findOne")
    public Emp findOne(String id){
        Emp emp = empService.querytOne(id);
        return emp;
    }
    //修改
    @ResponseBody
    @PostMapping("/emp/update")
    public  HashMap<String, Object> update(Emp emp, MultipartFile photo){
        HashMap<String, Object> map = new HashMap<>();
        if (photo!=null){
            //1 获取文件夹名字
            String filename = photo.getOriginalFilename();
            try {
                //文件上传阿里云
                ALiYunOSSUpload.upload(photo,filename);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //修改数据库图片地址
            String videourl="http://yingx-js.oss-cn-beijing.aliyuncs.com/yingx/video/"+filename;
            emp.setPath(videourl);
        }
        System.out.println(emp);
        try {
            empService.update(emp);
            map.put("msg","员工信息保存成功");
            map.put("state",true);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg","员工信息保存失败");
            map.put("state",false);
            throw new RuntimeException();
        }
    }
    //添加
    @ResponseBody
    @PostMapping("/emp/save")
    public  HashMap<String, Object> save(Emp emp,MultipartFile photo){
        HashMap<String, Object> map = new HashMap<>();
        if (photo!=null){
            //1 获取文件夹名字
            String filename = photo.getOriginalFilename();
            try {
                //文件上传阿里云
                ALiYunOSSUpload.upload(photo,filename);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //修改数据库图片地址
            String videourl="http://yingx-js.oss-cn-beijing.aliyuncs.com/yingx/video/"+filename;
            emp.setPath(videourl);
        }
        try {
            emp.setId(UUID.randomUUID().toString());
            empService.add(emp);
            map.put("msg","员工信息保存成功");
            map.put("state",true);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg","员工信息保存失败");
            map.put("state",false);
            throw new RuntimeException();
        }
    }
}
